package priv.books.controller;


import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import priv.books.pojo.Admin;
import priv.books.pojo.ResultMessage;
import priv.books.service.AdminService;

/**
 * @author Kris He
 * @date 2022/04/29 10:13
 **/
@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    AdminService adminService;

    @DeleteMapping("/deleteAdminInfo")
    public ResultMessage deleteInfo(Integer id, String ids) {
        try {
            // 调用service 修改方法
            boolean result = false;
            if(id != null && id != 0) {
                // 删除用户详情
                result=adminService.remove(new QueryWrapper<Admin>().eq("id", id));

            } else if(!StrUtil.isBlankOrUndefined(ids)) {
                // 删除用户详情
                result=adminService.remove(new QueryWrapper<Admin>().in("id", StrUtil.split(ids, ',', true, true)));
            }
            if(result) {
                return new ResultMessage(0, "操作成功！");
            }
            return new ResultMessage(207, "操作无响应！");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultMessage(500, "出现异常：" + e.getMessage());
        }
    }

    /**
     * 新增信息
     * @param
     * @return
     */
    @PostMapping("/insertInfo")
    public ResultMessage insertInfo(String userName, String nickName, String password) {
        try {
            // 设置修改时间
//            admin.setCreateDate(DateUtil.now());
//            admin.setUpdateDate(DateUtil.now());
            // 调用service 修改方法
            Admin admin = new Admin();
            admin.setUserName(userName);
            admin.setPassword(SecureUtil.md5(password));
            admin.setNickName(nickName);
            boolean result = adminService.save(admin);
            if(result) {
                return new ResultMessage(0, "操作成功！");
            }
            return new ResultMessage(207, "操作无响应！");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultMessage(500, "出现异常：" + e.getMessage());
        }
    }

    /**
     * 修改信息
     * @param
     * @return
     */
    @PostMapping("/updateInfo")
    public ResultMessage updateInfo(Integer id,String userName, String nickName, String password) {
        try {
            // 调用service 修改方法
            Admin admin = new Admin();
            admin.setId(id);
            admin.setUserName(userName);
            admin.setPassword(SecureUtil.md5(password));
            admin.setNickName(nickName);
            boolean result = adminService.updateById(admin);
            if(result) {
                return new ResultMessage(0, "操作成功！");
            }
            return new ResultMessage(207, "操作无响应！");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultMessage(500, "出现异常：" + e.getMessage());
        }
    }


}
